  // pages/personalCenter/checkIn/joinCheckIn/joinCheckIn.js
  var transDate = require('../../../../module/transDate.js');
  var distance = require('./module/distance');
  //允许距离，单位米
  var allDistance = 100;
  //用户经纬度
  var myLongitude = 0;
  var myLatitude = 0;
  //用户距离
  var myDistance = 0;
  //签到发起者的经纬度
  var newLa = 0;
  var newLo = 0;
  var app = getApp();
  //用来存本次签到记录
  var checkForm = [];
  Page({
      /**
       * 自定义函数
       */

      //清空变量
      pageInit: function() {
          //签到发起者的经纬度
          newLa = 0;
          newLo = 0;
          //用户经纬度
          myLongitude = 0;
          myLatitude = 0;
          //用户距离
          myDistance = 0;
      },

      //获取自己的位置
      locationInit: function() {
          var that = this;
          wx.getLocation({
              success: function(res) {
                  myLongitude = res.longitude;
                  myLatitude = res.latitude;
                  that.setData({
                      myLongitude: myLongitude
                  });
                  that.setData({
                      myLatitude: myLatitude
                  });
              },
          });
      },

      //自己登录和分享后第一次打开页面输入暗号函数
      loginOrShareSubmitCheckNum: function() {
          var that = this;
          //获取发起签到者数据
          wx.request({
              url: `https://wx.jingqi.ink/wxapp/checkin/999/${checkForm[checkForm.length - 1].check_num}`,
              method: 'GET',
              success(res) {
                  //先判断有没有这个暗号
                  if (res.data == [] || res.data == undefined || res.data == '') {
                      wx.showModal({
                          title: '提示',
                          content: '暗号错误，请输入正确的暗号',
                          showCancel: false,
                          confirmText: '我知道了',
                      })
                  } else {
                      console.log(res.data[0].check_stats);
                      //判断是否过了签到期限
                      if (res.data[0].check_stats == 0 || res.data[0].check_stats == '0') {
                          wx.showModal({
                              title: '提示',
                              content: '签到时间已结束',
                              showCancel: false,
                              confirmText: '我知道了',
                          })
                      } else {
                          //显示签到结果
                          that.setData({
                              show_page: 'content'
                          });
                          newLa = res.data[0].check_latitude;
                          newLo = res.data[0].check_longitude;
                          myDistance = distance(newLa, newLo, myLatitude, myLongitude);
                          if (myDistance < allDistance) {
                              checkForm[checkForm.length - 1].distance = myDistance;
                              checkForm[checkForm.length - 1].status = 1;
                          } else {
                              checkForm[checkForm.length - 1].distance = myDistance;
                              checkForm[checkForm.length - 1].status = 0;
                          }
                          //将签到记录表赋值给页面变量
                          that.setData({
                              form: checkForm
                          });
                          //将处理后的数据发给服务器
                          wx.request({
                              url: `https://wx.jingqi.ink/wxapp/user_checkin/${checkForm[checkForm.length - 1].check_num}`,
                              method: 'POST',
                              data: checkForm,
                              success(res) {}
                          })
                      }
                  }
              }
          })
      },

      /**
       * 页面函数
       */

      //暗号输入框
      check_num_input: function(e) {
          var res = e.detail.value
          //输入四位数后
          if (res.length >= 4) {
              //给签到记录表赋值
              checkForm[checkForm.length] = {};
              checkForm[checkForm.length - 1].check_num = res;
              checkForm[checkForm.length - 1].time = transDate(Date.now());
              checkForm[checkForm.length - 1].user_id = app.globalData.user_info.user_id;
              checkForm[checkForm.length - 1].user_name = app.globalData.user_info.user_name;
              checkForm[checkForm.length - 1].longitude = myLongitude;
              checkForm[checkForm.length - 1].latitude = myLatitude;
          }
      },

      //点击确定按钮后
      submit_check_num: function() {
          this.loginOrShareSubmitCheckNum();
      },

      //签到失败再次签到
      check_num_input_again: function(e) {
          var res = e.detail.value
          //输入四位数后
          if (res.length >= 4) {
              //给签到记录表赋值
              checkForm[checkForm.length] = {};
              checkForm[checkForm.length - 1].check_num = res;
              checkForm[checkForm.length - 1].time = transDate(Date.now());
              checkForm[checkForm.length - 1].user_id = app.globalData.user_info.user_id;
              checkForm[checkForm.length - 1].user_name = app.globalData.user_info.user_name;
              checkForm[checkForm.length - 1].longitude = myLongitude;
              checkForm[checkForm.length - 1].latitude = myLatitude;
          }
      },

      //再次点击确定按钮后
      submit_again_check_num: function() {
          var that = this;
          //将输入框清空
          this.setData({
              check_num_input_value_again: ''
          });
          //获取发起签到者数据
          wx.request({
              url: `https://wx.jingqi.ink/wxapp/checkin/999/${checkForm[checkForm.length-1].check_num}`,
              method: 'GET',
              success(res) {
                  //先判断有没有这个暗号
                  if (res.data == [] || res.data == undefined || res.data == '') {
                      wx.showModal({
                          title: '提示',
                          content: '暗号错误，请输入正确的暗号',
                          showCancel: false,
                          confirmText: '我知道了',
                      })
                  } else {
                      console.log(res.data[0].check_stats);
                      //判断是否过了签到期限
                      if (res.data[0].check_stats == 0 || res.data[0].check_stats == '0') {
                          wx.showModal({
                              title: '提示',
                              content: '签到时间已结束',
                              showCancel: false,
                              confirmText: '我知道了',
                          });
                      } else {
                          newLa = res.data[0].check_latitude;
                          newLo = res.data[0].check_longitude;
                          myDistance = distance(newLa, newLo, myLatitude, myLongitude);
                          if (myDistance < allDistance) {
                              checkForm[checkForm.length - 1].distance = myDistance;
                              checkForm[checkForm.length - 1].status = 1;
                          } else {
                              checkForm[checkForm.length - 1].distance = myDistance;
                              checkForm[checkForm.length - 1].status = 0;
                          }
                          //将签到记录表赋值给页面变量
                          that.setData({
                              form: checkForm
                          });
                          //将处理后的数据发给服务器
                          wx.request({
                              url: `https://wx.jingqi.ink/wxapp/user_checkin/${checkForm[checkForm.length - 1].check_num}`,
                              method: 'POST',
                              data: checkForm,
                              success(res) {}
                          })
                      }
                  }
              }
          })
      },

      /**
       * 页面的初始数据
       */
      data: {
          //默认显示输入暗号页面
          show_page: 'check'
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function(options) {
          //临时保存签到码
          var num = '';
          //设置页面状态码
          if ('temp' in options) { //转发的页面
              num = JSON.parse(options.temp);
              //给签到记录表赋值
              checkForm[checkForm.length] = {};
              checkForm[checkForm.length - 1].check_num = num;
              checkForm[checkForm.length - 1].time = transDate(Date.now());
              checkForm[checkForm.length - 1].user_id = app.globalData.user_info.user_id;
              checkForm[checkForm.length - 1].user_name = app.globalData.user_info.user_name;
              checkForm[checkForm.length - 1].longitude = myLongitude;
              checkForm[checkForm.length - 1].latitude = myLatitude;
              this.loginOrShareSubmitCheckNum();
              //取消显示输入框
              this.setData({
                  show_page: 'content'
              });
          } else {
              checkForm = [];
          }
      },

      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function() {

      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function() {
          wx.hideShareMenu({});
          //清空变量
          this.pageInit();
          //获取自己的位置
          this.locationInit();
          wx.setNavigationBarTitle({
              title: '签到',
          })
      },

      /**
       * 生命周期函数--监听页面隐藏
       */
      onHide: function() {

      },

      /**
       * 生命周期函数--监听页面卸载
       */
      onUnload: function() {

      },

      /**
       * 页面相关事件处理函数--监听用户下拉动作
       */
      onPullDownRefresh: function() {

      },

      /**
       * 页面上拉触底事件的处理函数
       */
      onReachBottom: function() {

      },

      /**
       * 用户点击右上角分享
       */
      onShareAppMessage: function() {

      }
  })